/*
 * Copyright 1999-2004 The Apache Software Foundation.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */ 

package org.apache.taglibs.standard.lang.jstl;

import java.lang.reflect.Array;
import java.util.List;
import java.util.Map;

/**
 *
 * <p>empty操作符的实现类
 * @author Nathan Abramson - Art Technology Group
 * @version $Change: 181177 $$DateTime: 2001/06/26 08:45:09 $$Author: pierred $
 **/

public class EmptyOperator
  extends UnaryOperator
{
  //-------------------------------------
  // 单例
  //-------------------------------------

  public static final EmptyOperator SINGLETON =
    new EmptyOperator ();

  //-------------------------------------
  /**
   *
   * 构造函数
   **/
  public EmptyOperator ()
  {
  }

  //-------------------------------------
  // 表达式的方法
  //-------------------------------------
  /**
   *
   * 返回代表此操作符的标签
   **/
  public String getOperatorSymbol ()
  {
    return "empty";
  }

  //-------------------------------------
  /**
   *
   * 用操作符计算语句的值
   **/
  public Object apply (Object pValue,
		       Object pContext,
		       Logger pLogger)
    throws ELException
  {
    // 判断pValue的值是否为null
    if (pValue == null) {
      return PrimitiveObjects.getBoolean (true);
    }

    //判断pValue的值是否是一个长度为0的字符串
    else if ("".equals (pValue)) {
      return PrimitiveObjects.getBoolean (true);
    }
    
    //判断pValue的值是否是一个长度为0的数组
    else if (pValue.getClass ().isArray () &&
	     Array.getLength (pValue) == 0) {
      return PrimitiveObjects.getBoolean (true);
    }

    //判断pValue的值是否是一个空的List集合
    else if (pValue instanceof List &&
	     ((List) pValue).isEmpty ()) {
      return PrimitiveObjects.getBoolean (true);
    }

    //判断pValue的值是否是一个空的Map集合
    else if (pValue instanceof Map &&
	     ((Map) pValue).isEmpty ()) {
      return PrimitiveObjects.getBoolean (true);
    }

    //如果以上条件都不成立的话，就返回非空,即false
    else {
      return PrimitiveObjects.getBoolean (false);
    }
  }

  //-------------------------------------
}
